package cn.droidlover.xdroidmvp.mvp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.trello.rxlifecycle2.components.support.RxFragment;

import butterknife.Unbinder;
import cn.droidlover.xdroidmvp.R;
import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.kit.KnifeKit;
import cn.droidlover.xdroidmvp.view.SimpleLoadDialog;

/**
 * Created by wanglei on 2016/12/29.
 */

public abstract class XFragment<P extends IPresent> extends RxFragment implements IView<P> {

    private VDelegate vDelegate;
    private P p;
    protected Activity context;
    public View rootView;
    protected LayoutInflater layoutInflater;


    private Unbinder unbinder;

    public View mainView;
    public View nodataView;


    public ImageView errorIv;
    public TextView errorTitleTv;
    public TextView errorDescTv;
    public TextView refreshTv;

    public ImageView leftIv;
    public TextView titleTv;
    public TextView rightTv;
    SimpleLoadDialog simpleLoadDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layoutInflater = inflater;
        if (rootView == null && getContentViewId() > 0) {
            rootView = mergeMainView();
            bindUI(rootView);
        } else {
            ViewGroup viewGroup = (ViewGroup) rootView.getParent();
            if (viewGroup != null) {
                viewGroup.removeView(rootView);
            }
        }

        return rootView;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (useEventBus()) {
            BusProvider.getBus().register(this);
        }
        bindEvent();
        initView();
        initData(savedInstanceState);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if (isNotWorkConnect()) {
            showNoNetPage();
        }
        getDataFromServer();
    }

    public boolean isNotWorkConnect() {
        if (Kits.NetWork.getNetworkTypeName(getActivity()) == Kits.NetWork.NETWORK_TYPE_DISCONNECT) {
            return true;
        } else {
            return false;
        }
    }


    public abstract void getDataFromServer();

    public void onFail(String message) {

    }

    @Override
    public void initView() {
        simpleLoadDialog = new SimpleLoadDialog(getActivity(), true);
        leftIv = rootView.findViewById(R.id.leftIv);
        titleTv = rootView.findViewById(R.id.titleTv);
        rightTv = rootView.findViewById(R.id.rightTv);
        errorIv = rootView.findViewById(R.id.errorIv);
        errorTitleTv = rootView.findViewById(R.id.errorTitleTv);
        errorDescTv = rootView.findViewById(R.id.errorDescTv);
        refreshTv = rootView.findViewById(R.id.refreshTv);
        refreshTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toRefresh();
            }
        });
    }

    @Override
    public void bindUI(View rootView) {
        unbinder = KnifeKit.bind(this, rootView);
    }

    protected VDelegate getvDelegate() {
        if (vDelegate == null) {
            vDelegate = VDelegateBase.create(context);
        }
        return vDelegate;
    }

    protected P getP() {
        if (p == null) {
            p = newP();
            if (p != null) {
                p.attachV(this);
            }
        }
        return p;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Activity) {
            this.context = (Activity) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        context = null;
    }

    @Override
    public boolean useEventBus() {
        return false;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (useEventBus()) {
            BusProvider.getBus().unregister(this);
        }
        if (getP() != null) {
            getP().detachV();
        }
        getvDelegate().destory();

        p = null;
        vDelegate = null;
    }

    @Override
    public int getOptionsMenuId() {
        return 0;
    }

    @Override
    public void bindEvent() {

    }

    private View mergeMainView() {
        View viewGroup = LayoutInflater.from(getActivity()).inflate(R.layout.basefragmentpage, null, false);
        FrameLayout baseMainLayout = viewGroup.findViewById(R.id.baseMainLayout);
        LinearLayout errorLayout = viewGroup.findViewById(R.id.errorLayout);
        int contentId = getContentViewId();
        mainView = LayoutInflater.from(getActivity()).inflate(contentId, null, false);
        baseMainLayout.addView(mainView);
        View errorView = mainView.findViewById(R.id.nodate_layout);
        if (errorView == null) {
            errorView = LayoutInflater.from(getActivity()).inflate(R.layout.nodata_layout, null, false);
            errorLayout.addView(errorView);
        }
        errorLayout.setVisibility(View.INVISIBLE);
        nodataView = errorLayout;
        return viewGroup;
    }

    @Override
    public boolean showNoDataPage() {
        mainView.setVisibility(View.INVISIBLE);
        nodataView.setVisibility(View.VISIBLE);
        errorIv.setImageResource(R.drawable.nodata);
        errorTitleTv.setText("暂无数据信息哦");
        errorDescTv.setText("请您稍后再来查看吧~~");
        refreshTv.setText("刷新");
        return true;
    }

    @Override
    public boolean showNoNetPage() {
        mainView.setVisibility(View.INVISIBLE);
        nodataView.setVisibility(View.VISIBLE);
        errorIv.setImageResource(R.drawable.nonet);
        errorTitleTv.setText("网络竟然崩溃了");
        errorDescTv.setText("主人别急，点击重新加载试试~~");
        refreshTv.setText("重新加载");
        return true;
    }

    @Override
    public void showMainView() {
        mainView.setVisibility(View.VISIBLE);
        nodataView.setVisibility(View.INVISIBLE);
    }

    public void toRefresh() {
        getDataFromServer();
    }

    @Override
    public void getIntentData() {

    }

    public void showLoadDialog() {
        simpleLoadDialog.show();
    }

    @Override
    public void disLoadDialog() {
        simpleLoadDialog.dismiss();
    }

    Toast mToast;

    @Override
    public void showToast(String content) {
        disLoadDialog();
        if (mToast == null) {
            mToast = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(content);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }

    @Override
    public void twinkCompleted() {

    }

    @Override
    public void loadFail() {
        twinkCompleted();
    }


    @Override
    public void toLogin() {
        Intent intent = new Intent();
        intent.setAction("activity.neet.to.login");
        startActivityForResult(intent, 0x0000);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0x0000) {
            getDataFromServer();
        }
    }
}
